package com.sunshine.controller;

import com.google.common.base.Preconditions;
import com.mysql.cj.util.StringUtils;
import com.sunshine.common.entity.Result;
import com.sunshine.common.enums.SubjectInfoEnum;
import com.sunshine.controller.convert.SubjectAnswerConvertDTO;
import com.sunshine.controller.convert.SubjectCategroyConvertDTO;
import com.sunshine.controller.convert.SubjectInfoConvertDTO;
import com.sunshine.controller.dto.SubjectInfoDTO;
import com.sunshine.domain.entity.SubjectAnswerBO;
import com.sunshine.domain.entity.SubjectCategroyBO;
import com.sunshine.domain.entity.SubjectInfoBO;
import com.sunshine.domain.service.SubjectInfoDomainService;
import com.sunshine.infra.basic.service.SubjectCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @version 1.0
 * @Author 夏百友
 * @Date 2025/11/7 17:47
 */

@RestController
@Slf4j
@RequestMapping("/subject")
public class SubjectController {

    @Resource
    private SubjectInfoDomainService subjectInfoDomainService;

    @Resource
    private SubjectCategoryService subjectCategoryService;

    @PostMapping("/add")
    public Result<Boolean> addSubjectCategory(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("controller.SubjectController.addSubjectCategory:参数{}", subjectInfoDTO);
            }
            Preconditions.checkArgument(!StringUtils.isNullOrEmpty(subjectInfoDTO.getSubjectName()), "题目名称不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectDifficult(), "难度不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectType(), "题目类型不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectParse(), "分数不能为空");
            Preconditions.checkArgument(CollectionUtils.isEmpty(subjectInfoDTO.getCategroryIds()), "分类id不能为空");
            Preconditions.checkArgument(CollectionUtils.isEmpty(subjectInfoDTO.getLabelIds()), "标签id不能为空");

            SubjectInfoBO subjectInfoBO = SubjectInfoConvertDTO.INSTANCE.convertInfoDtoToBo(subjectInfoDTO);
            List<SubjectAnswerBO> subjectAnswerBOS = SubjectAnswerConvertDTO.INSTANCE.convertListDtoToBo(subjectInfoDTO.getOptionlist());
            subjectInfoBO.setOptionlist(subjectAnswerBOS);
            subjectInfoDomainService.addSubjectInfo(subjectInfoBO);
            return Result.ok(true);
        } catch (Exception e) {
            log.error("controller.SubjectCategoryController.addSubjectCategory:异常 {}", e.getMessage(), e);
            return Result.fail();
        }
    }
}
